home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*/
- /* The Opus Computer-Based Conversation System */
- /* (c) Copyright 1986, Wynn Wagner III, All Rights Reserved */
- /*--------------------------------------------------------------------------*/
- /* */
- /* STRIP_Z: Filter Control-Z (CP/M style end-of-file marker) from text file */
- /* Renamed STRP_ALL */
- /* COMPILER: MicroSoft C, v4.0 */
- /* Modified 4/2/88 by J.D Bowman (135/38) to strip any ascii char. */
- /* Translated back into the C language from Cpascal on the same date <smile> */
- /*--------------------------------------------------------------------------*/
-
- #include <io.h>
- #include <stdio.h>
- #include <types.h>
- #include <stat.h>
- #include <malloc.h>
-
- static char *BUFFER_ERROR = "\r\nFile buffer error. Unable to continue.\r\n$";
-
-
-
-
- /*--------------------------------------------------------------------------*/
- /* EXTENDED HELP */
- /*--------------------------------------------------------------------------*/
- void extended_help()
- {
- bdos(9,"Strp_ALL removes any single character from a test file.\r\n$");
- bdos(9,"First, it creates a backup file (`.BAK') of the program...\r\n$");
- bdos(9,"then does the filtering.\r\n\n$");
- bdos(9," USAGE: strp_all filename decimal_char\r\n\n$");
- bdos(9," EXAMPLE: strp_z c:\\pascal\\files.bbs 12\r\n\n$");
- bdos(9," will strip all formfeeds from files.bbs .\r\n\n$");
- bdos(9," If no character is given on the command line, it is\r\n\n$");
- bdos(9," assumed you wish to strip the cpm eof (decimal 26).\r\n\n$");
-
- exit(1);
-
- }
-
- /*--------------------------------------------------------------------------*/
- /* STRMFE: Make File Extension */
- /*--------------------------------------------------------------------------*/
- void strmfe( new, old, extension )
- char *new, *old, *extension;
- {
- register int i;
-
- strcpy( new, old );
-
- for(i=0;i<79;i++)
- {
- if (!new[i])
- {
- new[i] = '.';
- new[i+1] = '\0';
- goto done;
- }
- else if (new[i]=='.')
- {
- new[i+1] = '\0';
- goto done;
- }
- }
- done:
- strcat( new, extension );
- } /* strmfe */
-
-
-
-
- /*--------------------------------------------------------------------------*/
- /* MAIN: Strip_Z */
- /*--------------------------------------------------------------------------*/
- main(argc,argv)
- int argc;
- char *argv[];
- {
- register int c;
- register int c1;
-
- FILE *source;
- FILE *dest;
-
- char *source_buffer;
- char *dest_buffer;
-
- char backup[82];
- struct stat *statptr;
-
-
-
- bdos(9,"STRP_ALL[.01] Modified Strip_Z by J.D. Bowman\r\n$");
- if (argc<2) extended_help();
-
- /*--------------------------------------------------------------------*/
- /* See if the requested file exists. */
- /*--------------------------------------------------------------------*/
- statptr = (struct stat *)malloc( sizeof(struct stat) );
- if (stat(argv[1],statptr))
- {
- free( statptr );
- bdos(9,"\r\n\nNO SUCH FILE\r\n$");
- extended_help();
- }
- free( statptr );
-
-
- /*--------------------------------------------------------------------*/
- /* Maintain a backup copy of the file. */
- /*--------------------------------------------------------------------*/
- strmfe( backup, argv[1], "Bak" );
- unlink( backup );
- if (rename(argv[1],backup))
- {
- bdos(9,"\r\n\nCAN'T CREATE BACKUP\r\n$");
- extended_help();
- }
-
-
- /*--------------------------------------------------------------------*/
- /* Open the source and destination files */
- /*--------------------------------------------------------------------*/
- source = fopen( backup, "rb" );
- if (!source)
- {
- bdos(9,"\r\nCAN'T OPEN SOURCE FILE\r\n$");
- extended_help();
- }
-
- dest = fopen( argv[1], "wb" );
- if (!dest)
- {
- bdos(9,"\r\nCAN'T OPEN DESTINATION FILE\r\n$");
- extended_help();
- }
-
-
- /*--------------------------------------------------------------------*/
- /* Setup some elephant buffers */
- /*--------------------------------------------------------------------*/
- c = _memavl() / 3;
- source_buffer = malloc(c);
- dest_buffer = malloc(c);
-
- if ((!source_buffer) || (!dest_buffer))
- {
- bdos(9,"\r\nMEMORY ERROR: unable to continue\r\n$");
- exit(1);
- }
-
- if (setvbuf(source,source_buffer,_IOFBF,c))
- {
- bdos(9,BUFFER_ERROR);
- exit(1);
- }
-
- if (setvbuf(dest,dest_buffer,_IOFBF,c))
- {
- bdos(9,BUFFER_ERROR);
- exit(1);
- }
-
-
-
- /*--------------------------------------------------------------------*/
- /* Copy the file */
- /*--------------------------------------------------------------------*/
- if(argc < 3)
- {
- while(1)
- {
- switch( c=getc(source))
- {
-
- case EOF : fclose(source);
- fclose(dest);
- free(source_buffer);
- free(dest_buffer);
- exit(0);
-
- case 0x1a : break;
-
- default : putc( c, dest );
-
- } /* switch */
- }
- }
- if(argc > 2)
- {
- c=atoi(argv[2]);
- if((c<1) || (c>255))
- {
- printf("Filter Character %d is out of Ascii Range (1 - 255)\n",c);
- extended_help();
- fclose(source);
- fclose(dest);
- free(source_buffer);
- free(dest_buffer);
- exit(1);
- }
- printf("Filtering ascii character %d (Hex %x)\n",c,c);
- while(1)
- {
- c1=getc(source);
- if(c1==EOF)
- {
- fclose(source);
- fclose(dest);
- free(source_buffer);
- free(dest_buffer);
- exit(0);
- }
- if(c1==c) continue;
-
- putc( c1, dest );
-
- }
-
- } /* end if argc >2 */
-
- }